package com.tim.app.ui.adapter.message;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.application.library.util.TimeUtil;
import com.application.library.widget.recycle.BaseRecyclerAdapter;
import com.application.library.widget.roundimg.RoundedImageView;
import com.tim.app.R;
import com.tim.app.constant.AppKey;
import com.tim.app.server.entry.Notice;
import com.tim.app.server.entry.User;
import com.tim.app.ui.activity.some_homepage.SomeoneHomepageActivity;
import com.tim.app.util.BitmapLoader;

import java.util.List;


/**
 * 通知
 */
public class NoticeAdapter extends BaseRecyclerAdapter<BaseRecyclerAdapter.BaseRecyclerViewHolder, Notice> {

    private Context mContext;

    public NoticeAdapter(Context mContext, List<Notice> mDataList) {
        super(mDataList);
        this.mContext = mContext;
    }

    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        BaseRecyclerViewHolder holder = null;
        holder = new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_notice, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(BaseRecyclerViewHolder mHolder, int position, Notice data) {
        if (data == null) {
            return;
        }
        final ViewHolder holder = (ViewHolder) mHolder;
        if (null != data.getUser() && !TextUtils.isEmpty(data.getUser().getImg())) {
            BitmapLoader.ins().loadImage(data.getUser().getImg(), holder.rivAvatar);
        }
        if (null != data.getUser() && !TextUtils.isEmpty(data.getUser().getNick())) {
            holder.tvNoticeTitle.setText(data.getUser().getNick());
        } else {
            holder.tvNoticeTitle.setText("");
        }
        if (!TextUtils.isEmpty(data.getContent())) {
            holder.tvNoticeTitleDetail.setText(data.getContent());
        } else {
            holder.tvNoticeTitleDetail.setText("");
        }
        if (!TextUtils.isEmpty(data.getTitle()) && !data.getType().equals(AppKey.TYPE_DYNAMIC)) {
            holder.tvNoticeContent.setVisibility(View.VISIBLE);
            holder.tvNoticeContent.setText(data.getTitle());
        } else {
            holder.tvNoticeContent.setText("");
            holder.tvNoticeContent.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(data.getDigest()) && !data.getType().equals(AppKey.TYPE_ARTICLE)) {
            holder.tvNoticeDesc.setVisibility(View.VISIBLE);
            holder.tvNoticeDesc.setText(data.getDigest());
        } else {
            holder.tvNoticeDesc.setText("");
            holder.tvNoticeDesc.setVisibility(View.GONE);
        }

        if (data.isRead()) {
            holder.rlContainer.setSelected(false);
        } else {
            holder.rlContainer.setSelected(true);
        }

        holder.tvNoticeTime.setText(TimeUtil.formatTimeRuleTwo(mContext, data.getTime()));

        holder.rivAvatar.setTag(data);
        holder.tvNoticeTitle.setTag(data);
        holder.rivAvatar.setOnClickListener(onClickListener);
        holder.tvNoticeTitle.setOnClickListener(onClickListener);
    }

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Notice data = (Notice) v.getTag();
            User user = new User();
            user.setName(data.getUser().getNick());
            user.setUserId(data.getUser().getUserId());
            SomeoneHomepageActivity.start(mContext, user, false);
        }
    };

    public class ViewHolder extends BaseRecyclerViewHolder {

        RelativeLayout rlContainer;
        RoundedImageView rivAvatar;
        TextView tvNoticeTitle;
        TextView tvNoticeTitleDetail, tvNoticeContent, tvNoticeDesc, tvNoticeTime;

        public ViewHolder(View itemView) {
            super(itemView);
            rlContainer = (RelativeLayout) itemView.findViewById(R.id.rlContainer);
            rivAvatar = (RoundedImageView) itemView.findViewById(R.id.rivAvatar);
            tvNoticeTitle = (TextView) itemView.findViewById(R.id.tvNoticeTitle);
            tvNoticeTitleDetail = (TextView) itemView.findViewById(R.id.tvNoticeTitleDetail);
            tvNoticeContent = (TextView) itemView.findViewById(R.id.tvNoticeContent);
            tvNoticeDesc = (TextView) itemView.findViewById(R.id.tvNoticeDesc);
            tvNoticeTime = (TextView) itemView.findViewById(R.id.tvNoticeTime);
        }

    }

}
